package com.wilschrader.demarche.units;

import com.wilschrader.demarche.Simulation;
import com.wilschrader.demarche.Unit;
import com.wilschrader.demarche.util.MersenneTwisterFast;

/**
 * PDS's protect a planet from bombardment. Oh, they also shoot.
 * 
 * @author Wil Schrader
 * @modifications
 * 	08152011	Updates for the unified unit class.
 * 
 */
public class PDS extends Unit {
	
	int attempts;

	public PDS(int combatValue, int attempts) {
		super(Unit.Type.Cannon, 0, new int[]{combatValue}, new int[]{1}, 0, false);
		this.attempts = attempts;
	}
	
	/**
	 * Overridden so that PDS' can fire multiple times when initial shots are missed.
	 */
	@Override
	public int doAttack(MersenneTwisterFast mt, Weapon weapon) {
		int fired = 0;
		int hits = 0;
		
		for (int shot = 0; shot < shotCount[0]; shot++) {
			for (int attempt = 0; attempt < attempts; attempt++) {
				fired++;
				if (mt.nextInt(10) <= (combatValue[0] -1)) {
					hits++;
					break;
				}
			}
		}
		
		Simulation.debugOutput("Unit (" + this.getClass().getSimpleName() + ") fired " + fired + " shots, hit " + hits + ".");
		
		return hits;
	}
	

	@Override
	public PDS clone() throws CloneNotSupportedException {
		return new PDS(combatValue[0], attempts);
	}

}
